'''
Created on May 15, 2012

@author: TANDA


questions for tali:

1. can we assume *all* parameters will be provided at each test-case?
2. we need to clean the arguments we created after every testcase or after all testcases for single line done
3. can we use 'exec' in order to clean vars we created (if not, can you hint me since I didn't find way to do so with eval)

'''

import test_manager
import sys
import pickle

class SerializedTestCase(object):
    '''
    classdocs
    '''
    
    def __init__(self, serialized_test_case):
        self.test_id = serialized_test_case[0] 
        self.args = serialized_test_case[1][0]
        self.test_cases = []
        
        for test_case in serialized_test_case[1][1]:
            self.test_cases.append(test_case)

def get_lines_to_test(file_path):
    f = open(file_path, 'r')
    res = []
    
    for line in f.readlines():
        res.append(line.split('|'))
    
    return res

def get_test_cases(file_path):
    f = open(file_path, 'r')
    
    test_cases = pickle.load(f)
    
    res = []
    
    args_dict = {}
    
    for item in test_cases.items():
        tc = SerializedTestCase(item)
        res.append(tc)
        args_dict[tc.test_id] = tc.args
        
    f.close()

    return (res, args_dict)

def get_test_cases_arg_names(test_cases):
    args_dict = {}
    for line in test_cases:
        args_dict[line.items()[0][0]] = line.items()[0][1][0]
        
    return args_dict
if __name__ == '__main__':
    
    args = sys.argv[1:]
    
    if (len(args) != 2):
        print('Usage:\n    python2.7 tester.py [file to test] [test cases file]')
        sys.exit()
    
    lines_to_test = get_lines_to_test(args[0])
    test_cases, args_dict = get_test_cases(args[1])
    
    manager = test_manager.TestManager()
    
    tests = {}
    
    for line in lines_to_test:
        test_id = int(line[0])
        line_to_test = line[1]
        t = manager.append_line(test_id, line_to_test, args_dict[test_id])
        tests[test_id] = t
        
    for single_test in test_cases:
        for single_test_test_case in single_test.test_cases:
            tests[single_test.test_id].add_test_case(single_test_test_case[0], single_test_test_case[1])
    
    manager.run_tests()    
    
         
    
